package com.dsj.gxrd.modules.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.constants.GxrdConstants;
import com.dsj.gxrd.modules.activity.domain.entity.RdScoreDetail;
import com.dsj.gxrd.modules.activity.domain.query.RdScoreDetailQuery;
import com.dsj.gxrd.modules.activity.domain.vo.RdScoreDetailInfoVo;
import com.dsj.gxrd.modules.activity.domain.vo.RdScoreDetailVo;
import com.dsj.gxrd.modules.activity.mapper.RdScoreDetailMapper;
import com.dsj.gxrd.modules.activity.service.RdScoreDetailService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author songpin566
 * @date 2023/10/12
 **/
@Service
public class RdScoreDetailServiceServiceImpl extends ServiceImpl<RdScoreDetailMapper, RdScoreDetail> implements RdScoreDetailService {

    @Autowired
    private RdScoreDetailMapper scoreDetailMapper;

    @Override
    public List<RdScoreDetailVo> selectScoreDetailList(RdScoreDetailQuery scoreDetailQuery) {
        List<RdScoreDetailVo> list = scoreDetailMapper.selectScoreDetailList(scoreDetailQuery, GxrdConstants.SCORE_DETAILS_LEVELS);
        //AjaxResult result = remoteUserService.dictType(GxrdConstants.DICT_TYPE_GXRD_LEVEL);
        //List<SysDictData> sysDictData = toDictData(result);
        //设置所属级别名称
        setLevelNames(list, scoreDetailQuery.getScoreYear());
        return list;
    }

    @Override
    public List<RdScoreDetailInfoVo> getInfo(Long id, Integer year) {
        return scoreDetailMapper.getInfo(id, year);
    }

    @Override
    public boolean insertOrUpdateScoreDetail(List<RdScoreDetail> scoreDetail) {
        if (CollectionUtils.isEmpty(scoreDetail)) {
            return false;
        }

        LambdaQueryWrapper<RdScoreDetail> wrapper = new LambdaQueryWrapper<>();
        RdScoreDetail rdScoreDetail = scoreDetail.get(0);
        //先根据用户id和年份删除信息，再全部添加
        wrapper.eq(RdScoreDetail::getScoreYear, rdScoreDetail.getScoreYear()).eq(RdScoreDetail::getUserId, rdScoreDetail.getUserId());
        boolean remove = remove(wrapper);
        for (RdScoreDetail detail : scoreDetail) {
            if (rdScoreDetail.getId() != null) {
                detail.setUpdateTime(LocalDateTime.now());
                detail.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
            } else {
                detail.setCreateTime(LocalDateTime.now());
                detail.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
            }
        }

        return saveOrUpdateBatch(scoreDetail);
    }

    private void setLevelNames(List<RdScoreDetailVo> list, Integer scoreYear) {
        StringBuilder s = new StringBuilder();
        for (RdScoreDetailVo rdScoreDetailVo : list) {
            rdScoreDetailVo.setScoreYear(scoreYear);
            String level = rdScoreDetailVo.getLevel();//0,1,2
            if (StringUtils.isNotEmpty(level)) {
                String[] split = level.split(",");
                for (String id : split) {
                    String name = GxrdConstants.DICT_TYPE_GXRD_LEVEL_MAP.get(id);
                    s.append(name).append(",");
                }
                s.deleteCharAt(s.length() - 1);
                rdScoreDetailVo.setLevelName(s.toString());
                s.delete(0, s.length());
            }
        }
    }


    private List<SysDictData> toDictData(AjaxResult result) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            Object o = result.get(AjaxResult.MSG_TAG);
            return objectMapper.readValue(objectMapper.writeValueAsString(o), new TypeReference<List<SysDictData>>() {
            });
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

}
